<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Order_product_Model extends CI_Model{
    function __construct()
    {
        parent::__construct();
    }
    function getAllData($num,$offset){
        $query = $this->db->get('order_product',$num,$offset);
      
        return $query->result();  
    } 
     function get_object($id)
    {
		$this->db->select('order_product.*,products.name as product_name,products.code as product_code');
		$this->db->join('products','products.products_id=order_product.product_id');
		$this->db->where('order_product_id',$id);
        $query = $this->db->get('order_product');
        return $query->row();
    }
    function get_product_from_order($order_id)
    {
		$this->db->select('order_product.*,products.name as product_name,products.code as product_code,image,color.name as color_name,size.name as size_name');
		$this->db->join('products','products.products_id=order_product.product_id');
		$this->db->join('color','order_product.color=color.color_id');
		$this->db->join('size','order_product.size=size.size_id');
		$this->db->where('order_id',$order_id);
        $query = $this->db->get('order_product');
        return $query->result();
    }
    
    function getSoluongFromId($id) {
        $this->db->where('bl_active', 1);
        $this->db->where('products_id', $id);
        $result = $this->db->get('products')->row();
        if(!empty($result)) {
            return $result->soluong;
        }
        return 0;
    }
    function update_soluong($id, $soluong) {
        $this->db->where('products_id', $id);
        $data = array('soluong' => $soluong);
        $query = $this->db->update('products', $data);
        if ($query) {
            return true;
        } else {
            return false;
        }
    }
    function delete($id){
          $this->db->where('order_product_id',$id);
          $query = $this->db->delete('order_product');
          if($query){
              return true;
          }else{
              return false;
          }
    }
	function deleted($id){
          $this->db->where('order_product_id',$id);
		  $data=array('bl_active'=>'-1');
          $query = $this->db->update('order_product',$data);
          if($query){
              return true;
          }else{
              return false;
          }
    }
    function get_list_permit(){
        return $this->db->get('permission')->result();
    }
   
   function saveData($order_product_id,$data){
             if($order_product_id!=0){
                    $this->db->where('order_product_id',$order_product_id);
                    if($this->db->update('order_product',$data)){
                         return true;
                    }else{
                         return false;
                    }
             }else{
                    if($this->db->insert('order_product',$data)){
                        return true;
                        
                    }else{
                        return false;
                    }
            }
    }
   
    function get_all_data()
    {
         
            $query = $this->db->get('order_product');
             return $query->result();
    }
   
    
    
  }
?>
